package labuladong.第三章.twoSum问题;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Demo02 {
    public static void main(String[] args) {
        int[] arr = {3, 1, 3, 6};
        System.out.println(Arrays.toString(twoSum(arr, 6)));
    }

    public static int[] twoSum(int[] nums, int target) {
        int n = nums.length;
        Map<Integer, Integer> index = new HashMap<>();
        for(int i=0; i<n; i++) {
            index.put(nums[i], i);
        }

        for(int i=0; i<n; i++) {
            int others = target - nums[i];
            if(index.containsKey(others) && index.get(others) != i) {
                return new int[] {i, index.get(others)};
            }
        }
        return new int[] {-1, -1};
    }
 }
